<?php

class cAppTwitter extends caApp implements iApp
{
	public function __construct(dtoTwitterAuth $paramSessionDTO)
	{
		parent::__construct(__DIR__);
		$this->sessionDTO = $paramSessionDTO;
	}

	/**
	* Get the redirect url
	*
	* @return string
	*/
	public function getRedirectUrl()
	{
		$url = NULL;

		$this->oauth = new OAuth(   $this->cfg['key']['consumer_key'],
                                            $this->cfg['key']['consumer_secret'],
                                            OAUTH_SIG_METHOD_HMACSHA1,
                                            OAUTH_AUTH_TYPE_URI);
		$request_token_info = $this->oauth->getRequestToken($this->cfg['url']['request_token']); 
	
		if (isset($request_token_info["oauth_token"])
                    &&
                    isset($request_token_info["oauth_token_secret"]))
		{
			$this->sessionDTO->oauth_token		= $request_token_info['oauth_token'];
			$this->sessionDTO->oauth_token_secret	= $request_token_info['oauth_token_secret'];
			$this->sessionDTO->access_token 	= FALSE;

			$url = 	$this->cfg['url']['authenticate'] .
                                "?oauth_token=" .
                                $request_token_info["oauth_token"];
		}

		return $url;
	}

	/**
	* Request for access token
	*
	* @return array keys - oauth_token, oauth_token_secret (optional)
	*/
	public function requestAccessToken($get_token)
	{
		// $get_token -> $_GET['oauth_token']

		if ( ! $this->sessionDTO->oauth_token
                    ||
                    ! $this->sessionDTO->oauth_token_secret
                    ||
                    ! isset($get_token)
                    ||
                    ($get_token != $this->sessionDTO->oauth_token))
		return NULL;

		$this->oauth = new OAuth(   $this->cfg['key']['consumer_key'],
                                            $this->cfg['key']['consumer_secret'],
                                            OAUTH_SIG_METHOD_HMACSHA1,
                                            OAUTH_AUTH_TYPE_URI);
		$this->oauth->setToken(	$this->sessionDTO->oauth_token,
					$this->sessionDTO->oauth_token_secret);

			$access_token_info = $this->oauth->getAccessToken($this->cfg['url']['access_token']);

			// get extra infos
			$params = array('user_id' => $access_token_info['user_id']);
			$extras = fopenCurl(uri_builder($this->cfg['fetch']['user_show'], $params));
			$extras = json_decode($extras);

			// save infos
			$this->sessionDTO->screen_name		= $access_token_info['screen_name'];
			$this->sessionDTO->user_id		= $access_token_info['user_id'];
			$this->sessionDTO->oauth_token		= $access_token_info['oauth_token'];
			$this->sessionDTO->oauth_token_secret	= $access_token_info['oauth_token_secret'];
			$this->sessionDTO->utc_offset		= $extras->utc_offset;
			$this->sessionDTO->name			= $extras->name;
			$this->sessionDTO->access_token 	= TRUE;

		return $this->sessionDTO;
	}

	/**
	* Get the access token from the internal session or request it
	*
	* @return array keys - oauth_token, oauth_token_secret (optional)
	*/
	public function getAccessToken($get_token = NULL)
	{
		$access_token = NULL;

		if ($this->sessionDTO->access_token)
		{
			$access_token = $this->sessionDTO;
		}
		else
		{
			$access_token = $this->requestAccessToken($get_token);
		}
		
		return $access_token;
	}
}

# EOF
